package com.sjx.onelinecord.ui

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.sjx.onelinecord.R
import com.sjx.onelinecord.adapter.BaseViewPagerAdapter
import kotlinx.android.synthetic.main.activity_view_pager_aty.*

class ViewPagerAty : AppCompatActivity() {

    private var mList: ArrayList<View> = ArrayList()
    private lateinit var adapter: BaseViewPagerAdapter
    private lateinit var view1: View
    private lateinit var view2: View
    private lateinit var view3: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager_aty)
        initView()

        //预加载
        viewPager.offscreenPageLimit = mList.size
        adapter = BaseViewPagerAdapter(mList)
        viewPager.adapter = adapter

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                selectPoint(position)
            }

        })
    }


    private fun initView() {
        view1 = View.inflate(this, R.layout.view_pager_one, null)
        view2 = View.inflate(this, R.layout.view_pager_two, null)
        view3 = View.inflate(this, R.layout.view_pager_three, null)
        mList.add(view1)
        mList.add(view2)
        mList.add(view3)
    }

    private fun selectPoint(position: Int) {
        when (position) {
            0 -> {
                point_one.setImageResource(R.drawable.img_guide_point_p)
                point_two.setImageResource(R.drawable.img_guide_point)
                point_three.setImageResource(R.drawable.img_guide_point)
            }

            1 -> {
                point_one.setImageResource(R.drawable.img_guide_point)
                point_two.setImageResource(R.drawable.img_guide_point_p)
                point_three.setImageResource(R.drawable.img_guide_point)
            }

            2 -> {
                point_one.setImageResource(R.drawable.img_guide_point)
                point_two.setImageResource(R.drawable.img_guide_point)
                point_three.setImageResource(R.drawable.img_guide_point_p)
            }

        }
    }
}